package Buffered_stream_package1;

import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Case_demo2 {
    public static void main(String[] args) throws IOException {
        //需求：把《出师表》的文章顺序进行恢复到一个新文件中

        //1、读取数据
        BufferedReader br = new BufferedReader(new FileReader("/Users/zhaoxiaoyong/Documents/Java/" +
                "IO_stream_project/Buffered_stream_module/src/rough_draft/copy.txt"));

        String line;
        TreeMap<Integer, String> tm = new TreeMap<>();
        while ((line = br.readLine()) != null) {
            String[] arr = line.split("\\.");
            //0:序号    1：内容
            tm.put(Integer.parseInt(arr[0]), arr[1]);
        }

        br.close();

        //2、写出数据
        BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/zhaoxiaoyong/Documents/Java/" +
                "IO_stream_project/Buffered_stream_module/src/rough_draft/copy2.txt"));

        Set<Map.Entry<Integer, String>> entries = tm.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            bw.write(key + "." + value);
            bw.newLine();
        }
        bw.close();

    }
}
